package com.hison.courseselectionsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hison.courseselectionsystem.domain.Course;
import com.hison.courseselectionsystem.mapper.CourseMapper;
import com.hison.courseselectionsystem.service.CourseService;
import com.hison.courseselectionsystem.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author hison
 * @description 针对表【course】的数据库操作Service实现
 * @createDate 2023-11-16 21:53:51
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Resource
    private  CourseMapper courseMapper;
    /**
     * 添加课程
     * @param course
     * @return {@link Result}
     */
    @Override
    public Result add(Course course) {
        int i = courseMapper.insert(course);
        return i > 0?Result.ok("添加成功"):Result.fail("添加失败");
    }

    @Override
    public Result delete(Integer id) {
        int i = courseMapper.deleteById(id);
        return i > 0 ? Result.ok("删除成功"):Result.fail("删除失败");
    }

    @Override
    public Result update(Course course) {
        boolean b = updateById(course);
        return b ? Result.ok("更新成功"):Result.fail("更新失败");
    }

    @Override
    public Result findById(Integer id) {
        Course course = courseMapper.selectById(id);
        if (course == null) {
            return Result.fail("查找失败，没有该课程");
        }
        return Result.ok(course);
    }

    @Override
    public Result findByName(String name) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("course_name", name);
        Course course = courseMapper.selectOne(wrapper);
        if (course  != null ) {
            return Result.ok(course);
        }
        return Result.fail("没有该课程");
    }

    @Override
    public Result getAllCourse() {
        return Result.ok(courseMapper.selectList(null));
    }


}
